package com.lyrcsoft;

import com.lyrcsoft.annotation.LyrcsoftConditionOnClass;
import java.util.Map;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 功能描述
 *
 * @author fujc-dev@qq.com
 * @date: 2023年11月01日 13:51
 */

public class LyrcsoftOnClassCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        try {
            // 怎么拿到注解的值呢？Spring已经帮我们做了
            Map<String, Object> attributes = metadata.getAnnotationAttributes(LyrcsoftConditionOnClass.class.getName());
            String className = (String) attributes.get("value");
            context.getClassLoader().loadClass(className);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}
